package com.itany.book.controller;

import com.github.pagehelper.PageInfo;
import com.itany.book.pojo.Permission;
import com.itany.book.pojo.Role;
import com.itany.book.service.RoleService;
import com.itany.book.vo.ResponseResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.security.Permissions;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:yaozhiyuan0117@163.com
 * Time:2019/12/26 18:10
 * Describe:
 * version:1.0
 */


@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/findAll")
    public Map findAll(Integer page, @RequestParam(defaultValue = "1") Integer rows, String name){
        PageInfo<Role> info = roleService.findAll(page, rows, name);
        Map map = new HashMap<>();
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }

    @RequestMapping("/findById")
    public ResponseResult findById(Integer id){
        Role role = roleService.findById(id);
        return ResponseResult.success(role);
    }

    @RequestMapping("/modifyRole")
    public ResponseResult modifyRole(Integer id,
                                     String name,
                                     @RequestParam("permissions[]") String[] permissions){
        roleService.modifyRole(id, name,permissions);
        System.out.println("---------");
        System.out.println(id);
        System.out.println(name);
        System.out.println(Arrays.toString(permissions));
        return ResponseResult.success();
    }

    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(Integer id){
        roleService.deleteById(id);
        return ResponseResult.success();
    }

    @RequestMapping("/findRoles")
    @ResponseBody
    public List<Role> findRoles(){
        List<Role> roles = roleService.findRoles();
        return roles;
    }
}
